// pages/reportList.js
import * as FetchAPI from '../../wxapi/api/dispatch.js'
import * as Utils from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: Utils.parseTime(new Date(), '{y}-{m}-{d}') + '调度汇总',
    loading: false,
    disabled: false,
    dispatchInfo: {
      totalNum: '',
      plannedNum: '',
      noPlanNum: '',
    },
    repeartFalg: true, // 重复请求
    reportListData: []
  },
  // 登出
  toLoginOut() {
    let that = this
    wx.showToast({
      title: "登出成功",
      icon: "none",
      duration: 1000
    })
    setTimeout(() => {
      wx.setStorageSync('Authorization', '')
      wx.redirectTo({
        url: '../login/login'
      })
    }, 1000)
  },
  toReportDetailOne(e) {
    console.log('e.currentTarget', e.currentTarget)
    let caroption = {
      type: 'plannedNum',
      item: e.currentTarget.dataset.item
    }
    let caroptionstr = JSON.stringify(caroption)
    console.log('caroptionstr', caroptionstr)
    if (e) {
      wx.navigateTo({
        url: '../reportDetail/reportDetail?caroption=' + caroptionstr,
      })
    }
  },
  toReportDetailTwo(e) {
    console.log('e.currentTarget', e.currentTarget)
    let caroption = {
      type: 'noPlanNum',
      item: e.currentTarget.dataset.item
    }
    let caroptionstr = JSON.stringify(caroption)
    console.log('caroptionstr', caroptionstr)
    if (e) {
      wx.navigateTo({
        url: '../reportDetail/reportDetail?caroption=' + caroptionstr,
      })
    }
  },
  // 调度汇总查询
  getListMock() {
    const that = this
    that.setData({
      dispatchInfo: {
        totalNum: '1000',
        plannedNum: '500',
        noPlanNum: '21',
      }
    })
    that.setData({
      reportListData: [that.data.dispatchInfo]
    })
    console.log('reportListData', that.data.reportListData)
  },
  getList() {
    const that = this
    const userInfo = wx.getStorageSync('userInfo')
    const toParams = {
      companyId: userInfo.manager.companyId
    }
    wx.showLoading({
      title: '加载中...',
      icon: 'loading',
      mask: true
    })
    FetchAPI.getDispatchInfo(toParams).then(res => {
      if (res.data) {
        that.setData({
          dispatchInfo: res.data
        })
        that.setData({
          reportListData: [res.data]
        })
        console.log('reportListData', that.data.reportListData)
      }
      wx.hideLoading()
      that.setData({loading: false,disabled: false})
    }).catch(errTwo => {
      wx.hideLoading()
      that.setData({loading: false,disabled: false})
      console.log('error', errTwo)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log('reportListData', this.data.reportListData)
    // 获取token
    let Authorization = wx.getStorageSync('Authorization');
    if (!Authorization) {
      wx.showToast({
        title: "请先登录",
        icon: "none",
        duration: 2000
      })
      wx.redirectTo({
        url: '../login/login'
      })
    } else {
    this.getList()
    // this.getListMock()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('onShow()-', this.data.refresh)
    if(this.data.refresh){
      this.setData({
        refresh: false
      })
      this.onLoad();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})